package com.pku.jtestx86.popup.popup.actions;

import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.TreeSelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PartInitException;

import com.pku.jtestx86.views.ASTView;
import com.pku.jtestx86.views.CFGView;
import com.pku.ss.ui.CFGExplorer;

public class CFGAction implements IObjectActionDelegate {

	private Shell shell;
	private IWorkbenchPart targetPart;
	
	public CFGAction() {
		super();
	}
	@Override
	public void run(IAction arg0) {
		TreeSelection selection=(TreeSelection) this.targetPart.getSite().getSelectionProvider().getSelection();
		int segmentCount=selection.getPaths()[0].getSegmentCount();
		org.eclipse.jdt.internal.core.CompilationUnit compilationUnit=(org.eclipse.jdt.internal.core.CompilationUnit) selection.getPaths()[0].getSegment(segmentCount-1);
		//CFGExplorer.Init(compilationUnit);
		try {
			CFGView viewPart = (CFGView) this.targetPart.getSite().getWorkbenchWindow().getActivePage().showView("com.pku.jtestx86.views.CFGView");
			viewPart.show(compilationUnit);
		} catch (PartInitException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//System.out.println(viewPart.getClass());
		//viewPart.show(compilationUnit);
	}

	@Override
	public void selectionChanged(IAction arg0, ISelection arg1) {
		// TODO Auto-generated method stub

	}

	@Override
	public void setActivePart(IAction action, IWorkbenchPart targetPart) {
		// TODO Auto-generated method stub
		shell = targetPart.getSite().getShell();
		this.targetPart=targetPart;
	}

}
